package com.firepongo.chaos.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.firepongo.chaos.app.api.data.SortData;
import com.firepongo.chaos.app.api.entity.Sort;
import com.firepongo.chaos.app.api.service.ISortService;
import com.firepongo.chaos.app.helper.PageHelper;
import com.firepongo.chaos.app.service.ConvertService;
import com.firepongo.chaos.app.service.mapper.SortMapper;
import com.firepongo.chaos.model.data.UpdateData;
import com.firepongo.chaos.model.dto.mu.MU;
import com.firepongo.chaos.model.dto.page.PageList;
import com.firepongo.chaos.model.dto.page.PageQueryDto;
import com.firepongo.chaos.model.entity.Table;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* <p>
*  服务实现类
* </p>
*
* @author G.G
* @since 2021-08-05
*/
@Slf4j
@Component
public class SortServiceImpl extends ServiceImpl<SortMapper, Sort> implements ISortService {
	@Autowired
	private ConvertService convertService;

	@Override
	public MU insertModel(SortData data) {
		Sort entity = (Sort) convertService.convertToMuModel(data, Sort.class);
		return save(entity) ? MU.of(entity.getMu()) : null;
	}

	@Override
	public boolean deleteModel(MU data) {return removeById(data.getMu());}

	@Override
	public boolean updateModelByMU(UpdateData<SortData> data) {
		Sort entity = (Sort) convertService.convertToMuModel(data.getData(), Sort.class);
		return update(entity, new UpdateWrapper<Sort>().eq(Table.MU, data.getMu()));
	}

	@Override
	public SortData selectByMU(MU data) {
		return (SortData) convertService.convertToDTO(getOne(new QueryWrapper<Sort>()
			.eq(Table.MU, data.getMu())), SortData.class);
	}

	@Override
	public List<SortData> selectByData(SortData data) {
		QueryWrapper<Sort> query = new QueryWrapper<Sort>();
		query.lambda().eq(!StringUtils.isEmpty(data.getMu()), Sort::getMu, data.getMu());
		query.orderByDesc(Table.ID);
		return convertService.convertToDTO(list(query), SortData.class);
	}

	@Override
	public PageList<SortData> selectByPage(PageQueryDto<SortData> pageData) {
		QueryWrapper<Sort> query = new QueryWrapper<Sort>();
		query.lambda().eq(!StringUtils.isEmpty(pageData.getData().getMu()), Sort::getMu, pageData.getData().getMu());
		query.orderByDesc(Table.ID);
		return PageHelper.pageList(page(PageHelper.page(pageData), query), SortData.class);

	}

}
